<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <meta name="viewport" content="width=device-width,initial-scale=1.0">
</head>
<body>

<label id="content"></label>
<button onclick="initConfig()">点击获取微信票据</button>
<button onclick="scan()">扫一扫</button>
<button onclick="createOrder()">获取微信支付配置</button>
<button onclick="callWxPay()">调起微信支付</button>
</body>
<script src="utils.js"></script>
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
<script src="http://res.wx.qq.com/open/js/jweixin-1.2.0.js"></script>
<script>
    function initConfig() {
        let params = getUrlParams();
        let uid = params["uid"];
        let url = window.location.href;
        axios({
            method: "get",
            url: "http://imopei.95by.cn/api/wx/getWxJsConfig?uid=" + uid,
            headers: {
                "currentUrl": url
            }
        }).then(res => {
            if (res.data.code == 200) {
                console.log(res.data.data);
                let config = res.data.data;
                wx.config(config);
            }
        }).catch(err => {
            alert(JSON.stringify(err));
        });
    }

    function createOrder() {
        var params = {
            productId: 3,
            totalPrice: 20,
            finalPrice: 20,
            buyNum: 1,
            template: "",
            addressId: 1
        };
        let queryStr = "";
        for (let key in params) {
            queryStr = queryStr + key + "=" + params[key] + "&";
        }
        queryStr = queryStr.substring(0, queryStr.length - 1);
        console.log(queryStr);

        axios({
            method: 'get',
            url: "http://imopei.95by.cn/api/order/createOrder?" + queryStr,
            headers: {
                "token": token
            }
        }).then(res => {
            console.log(res);
            if (res.data.code == 200) {
                pay_config = res.data.data;
                alert(JSON.stringify(pay_config));
            } else {
                console.log(res.data.data);
            }
        }).catch(err => {
            console.log(err);
        });
    }

    var pay_config = {};

    function callWxPay() {
        function onBridgeReady() {
            alert("3");
            try {
                WeixinJSBridge.invoke(
                    'getBrandWCPayRequest', pay_config,
                    function (res) {
                        alert(JSON.stringify(res));
                        if (res.err_msg == "get_brand_wcpay_request:ok") {
                            alert("111");
                        }     // 使用以上方式判断前端返回,微信团队郑重提示：res.err_msg将在用户支付成功后返回    ok，但并不保证它绝对可靠。
                    }
                );
            } catch (e) {
                alert(JSON.stringify(e));
                console.error(e);
            }

        }

        if (typeof WeixinJSBridge == "undefined") {
            alert("1");
            if (document.addEventListener) {
                document.addEventListener('WeixinJSBridgeReady', onBridgeReady, false);
            } else if (document.attachEvent) {
                document.attachEvent('WeixinJSBridgeReady', onBridgeReady);
                document.attachEvent('onWeixinJSBridgeReady', onBridgeReady);
            }
        } else {
            alert("2");
            onBridgeReady();
        }
    }


    var token = localStorage["token"];

    /**
     * 扫码接口
     */
    function scan() {
        wx.scanQRCode({
            needResult: 1, // 默认为0，扫描结果由微信处理，1则直接返回扫描结果，
            scanType: ["Code128"], // 可以指定扫二维码还是一维码，默认二者都有
            success: function (res) {
                var result = res.resultStr; // 当needResult 为 1 时，扫码返回的结果
                alert(result);
            }
        });
    }

    window.onload = function () {
        document.getElementById("content").innerText = new Date().getTime();
    }

</script>
</html>